package com.jinlejia.erp.ui.common;

import android.app.AlertDialog;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.freedom.yefeng.yfrecyclerview.YfListRecyclerView;
import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.SheetAdapter;

import java.util.ArrayList;
import java.util.Map;

/**
 * @author SongJiaTai
 * @description:底部输入选择框
 * @date :2019/6/20 17:54
 */
public class SheetSelectEt extends AlertDialog{
    private Context context;
    ArrayList<Map<String,Object>> data;
    SheetAdapter adapter;


    public SheetSelectEt(Context context, ArrayList<Map<String,Object>> d) {
        super(context);
        this.context = context;
        this.data = d;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Window window = this.getWindow();
        window.setContentView(R.layout.dialog_sheet_select_et);
        window.setGravity(Gravity.BOTTOM);
        window.setBackgroundDrawable(new ColorDrawable());

        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用

        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, (int) (d.getHeight() * 0.4));

        YfListRecyclerView recyclerView = window.findViewById(R.id.yf_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setHasFixedSize(true);
        adapter = new SheetAdapter(data);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener((view, o) -> {
            if( selectListener != null ){
                selectListener.select(o);
            }
        });

        TextView tvSure = window.findViewById(R.id.tvSure);
        final EditText editext = window.findViewById(R.id.editext);
        editext.setRawInputType(Configuration.KEYBOARD_QWERTY);
        tvSure.setOnClickListener(v -> {
            if (onSureListener!=null){
                InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm!=null && window!=null){
                    imm.hideSoftInputFromWindow(window.getDecorView().getWindowToken(), 0);
                }
                // 隐藏软键盘
                onSureListener.select(editext.getText().toString());
            }
        });

    }


    public interface OnSelectListener {
        void select(Object o);
    }

    public interface OnSureListener{
        void select(String name);
    }


    private OnSelectListener selectListener;
    private OnSureListener onSureListener;

    public void setOnSelectListener(OnSelectListener listener) {
        this.selectListener = listener;
    }

    public void setOnSureListener(OnSureListener listener){
        this.onSureListener = listener;
    }
}
